package com.yhm.action;

import com.opensymphony.xwork2.ActionSupport;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.yhm.barcode.JniPplb;
import com.yhm.barcode.createCode128;
import com.yhm.model.OrderFrom;
import com.yhm.service.OrderManager;
import com.yhm.util.page.OrderPage;

@SuppressWarnings("serial")
@Component("order")
@Scope("prototype")
public class OrderAction extends ActionSupport {
	
	private String companyNum;
	private String serial_number;
	private String customNum;
	private String customType;
	private String marks;
	private String movement;
	private String num;
	private String selABCDE = "0";
	private String ifImportant = "0000";
	private String sign;
	
	private OrderManager orderManager;
	private String json;
	private OrderPage page;

	private String ck_com;
	
	private String begin_time;
	private String end_time;
	private String pwd;
	
	//添加页面
	public String add(){
		return SUCCESS;
	}
	//管理页面
	public String manager(){
		return SUCCESS;
	}
	//条码页面
	public String bar_prn(){
		return SUCCESS;
	}
	
	//订单添加
	public String order_add() {
		if ((this.companyNum.length() == 0) && serial_number.length() == 0) {
			json = "{\"status\":1,\"mess\":\"公司编码与流水号不能为空！！\"}";
			return "json";
		}
		String comNum = "";
		if ((this.companyNum.length() != 3)) {
			json = "{\"status\":1,\"mess\":\"公司编码必须为3位！！\"}";
			return "json";
		}
		if (serial_number.length() == 1) {
			serial_number = "000" + serial_number;
		}
		if (serial_number.length() == 2) {
			serial_number = "00" + serial_number;
		}
		if (serial_number.length() == 3) {
			serial_number = "0" + serial_number;
		}
		if (serial_number.length() != 4) {
			json = "{\"status\":1,\"mess\":\"流水号不能超过四位！！\"}";
			return "json";
		}
		comNum = this.companyNum + gettime() + this.selABCDE + serial_number;
		if (ifImportant.length() == 0) {
			comNum = comNum + "0000";
		} else if (ifImportant.length() == 4) {
			comNum = comNum + ifImportant;
		} else {
			json = "{\"status\":1,\"mess\":\"重要订单编码只能为4位或不填！！\"}";
			return "json";
		}
		Date date = new Date();
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
		String regtime = df1.format(date);
		OrderFrom of = new OrderFrom();
		of.setCompanyNum(comNum);
		of.setCustomNum(this.customNum);
		of.setCustomType(this.customType);
		of.setMarks(this.marks);
		of.setMovement(this.movement);
		of.setNum(this.num);
		of.setMakeTime(regtime);
		of.setDepartment("写字楼");
		of.setQc1("0");
		of.setQc2("0");
		of.setSign(sign);
		
		json = orderManager.order_add(comNum,of);
		String file2 = createCode128.createCode(comNum);
		//String pplb_title = JniPplb.pplb_prn(comNum);
		String pplb_title = "0";
		json = json + "\"comNum\":\"订单号："+comNum+"\" , \"file\":\""+file2+"\" , \"pplb_title\":\""+pplb_title+"\" " + "}";
		return "json";
	}

	//条码生成
	public String bar_re_prn(){
		String file2 = createCode128.createCode(companyNum);
		String pplb_title = JniPplb.pplb_prn(companyNum);
		json = "{\"comNum\":\"订单号："+companyNum+"\" , \"file\":\""+file2+"\" ,\"pplb_title\":\""+pplb_title+"\"}";
		return "json";
	}
	
//	public String aaa(){
//		Date date = new Date();
//		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
//		String regtime = df1.format(date);
//		String s = "1221210A1211";
//		for(int i=1;i<9999;i++){
//			String x = String.valueOf(i);
//			if(x.length()==3) x = "0"+x;
//			if(x.length()==2) x = "00"+x;
//			if(x.length()==1) x = "000"+x;
//			OrderFrom of = new OrderFrom();
//			of.setCompanyNum(s+x);
//			of.setMakeTime(regtime);
//			of.setDepartment("写字楼");
//			of.setQc1("0");
//			of.setQc2("0");
//			orderManager.order_add(s+x,of);
//		}
//		return "json";
//	}
	
	//获取订单信息
	public String get_order() {
		json = orderManager.get_order(companyNum);
		String file2 = createCode128.createCode(companyNum);
		json = json + "\"comNum\":\"订单号："+companyNum+"\" , \"file\":\""+file2+"\" " + "}";
		return "json";
	}

	//修改订单
	public String order_update() {
		json = orderManager.order_update(companyNum, customNum, customType, movement, marks, num);
		return "json";
	}
	
	
	//删除订单
	public String order_del() {
		json = orderManager.order_del(companyNum);
		return "json";
	}
	
	//批量删除订单
	public String order_del_s() {
		String[] ck = ck_com.split(",");
		json = orderManager.order_del_s(ck);
		return "json";
	}
	
	//按月时间删除
	public String order_mouth_del(){
		if(begin_time.length()==0||end_time.length()==0){
			json = "{\"status\":1,\"mess\":\"请填写时间！！\"}";
		}
		if(pwd.equals("569980")){
			json = orderManager.order_mouth_del(begin_time, end_time);
		}else{
			json = "{\"status\":1,\"mess\":\"许可密码错误！！\"}";
		}
		return "json";
	}
	

	//显示所有未完成订单信息
	public String order_list() throws ParseException {
		if(page == null)
			page = new OrderPage();
		json = orderManager.get_order_list(page);
		return "json";
	}

	//追溯信息
	public String order_trace(){
		json = orderManager.get_order_trace(companyNum);
		return "json";
	}
	

	
	/*
	 * get 12/06
	 */
	public static String gettime() {
		Calendar now = Calendar.getInstance();
		int year = now.get(1);
		int month = now.get(2) + 1;
		String syear = String.valueOf(year).substring(2);
		String smonth = String.valueOf(month);
		String smonth2 = smonth;
		if (smonth.length() == 1) {
			smonth2 = "0" + smonth;
		}

		String com = syear + smonth2;
		return com;
	}

	/**
	 * get 1206
	 * 
	 * @return
	 */
	public static String getdate() {
		Calendar now = Calendar.getInstance();
		int year = now.get(1);
		int month = now.get(2) + 1;
		String syear = String.valueOf(year);
		String smonth = String.valueOf(month);
		String smonth2 = smonth;
		if (smonth.length() == 1) {
			smonth2 = "0" + smonth;
		}
		String com = syear + smonth2;
		return com;
	}
	
	
	
	
	
	
	@Resource(name = "orderManager")
	public void setOrderManager(OrderManager orderManager) {
		this.orderManager = orderManager;
	}
	
	
	public String getCompanyNum() {
		return companyNum;
	}
	public void setCompanyNum(String companyNum) {
		this.companyNum = companyNum;
	}
	public String getCustomNum() {
		return customNum;
	}
	public void setCustomNum(String customNum) {
		this.customNum = customNum;
	}
	public String getCustomType() {
		return customType;
	}
	public void setCustomType(String customType) {
		this.customType = customType;
	}
	public String getMarks() {
		return marks;
	}
	public void setMarks(String marks) {
		this.marks = marks;
	}
	public String getMovement() {
		return movement;
	}
	public void setMovement(String movement) {
		this.movement = movement;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getSelABCDE() {
		return selABCDE;
	}
	public void setSelABCDE(String selABCDE) {
		this.selABCDE = selABCDE;
	}
	public String getIfImportant() {
		return ifImportant;
	}
	public void setIfImportant(String ifImportant) {
		this.ifImportant = ifImportant;
	}
	public String getSerial_number() {
		return serial_number;
	}
	public void setSerial_number(String serial_number) {
		this.serial_number = serial_number;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	public OrderPage getPage() {
		return page;
	}
	public void setPage(OrderPage page) {
		this.page = page;
	}
	public String getCk_com() {
		return ck_com;
	}
	public void setCk_com(String ck_com) {
		this.ck_com = ck_com;
	}
	public String getBegin_time() {
		return begin_time;
	}
	public void setBegin_time(String begin_time) {
		this.begin_time = begin_time;
	}
	public String getEnd_time() {
		return end_time;
	}
	public void setEnd_time(String end_time) {
		this.end_time = end_time;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getSign() {
		return sign;
	}
	public void setSign(String sign) {
		this.sign = sign;
	}
}
